package com.weixin.junit;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupTest {
    static String url="http://www.mogujie.com/";
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        
      
    	article();
      
    }

    /**
     * 获取指定HTML 文档指定的body
     * @throws IOException
     */
    private static void BolgBody() throws IOException {         
        // 从 URL 直接加载 HTML 文档
        Document doc = Jsoup.connect(url).get();
        Elements ListDiv = doc.getElementsByAttributeValue("class","nav_wrap");
        for (Element element :ListDiv) {          
            Elements Clinks=element.select("dt>a");
            for(Element link:Clinks)
            {
            	System.out.println(link.text());
            }
        }  
        
    }

    /**
     * 获取博客上的文章标题和链接
     */
    public static void article() {
        Document doc;
        try {
            doc = Jsoup.connect("http://product.weather.com.cn/alarm/grepalarm_cn.php?_=1541846038959").get();
            System.out.println(doc);
            Elements ListDiv = doc.getElementsByAttributeValue("class","postTitle");
            for (Element element :ListDiv) {
                Elements links = element.getElementsByTag("a");
                for (Element link : links) {
                    String linkHref = link.attr("href");
                    String linkText = link.text().trim();
                    System.out.println(linkHref);
                    System.out.println(linkText);
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    /**
     * 获取指定博客文章的内容
     */
    public static void Blog() {
        Document doc;
        try {
            doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get();
            Elements ListDiv = doc.getElementsByAttributeValue("class","postBody");
            for (Element element :ListDiv) {
                System.out.println(element.html());
            }
        } catch (IOException e) {
        }
    }
}